package com.tistory.wonsama.helper;

import java.io.FileInputStream;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;

import com.tistory.wonsama.util.WLog;

public class WExcel {
	
	/**
	 * HSSFWorkbook : XLS 만 사용가능 함.
	 * @param filePath 파일경로 
	 * @return 처리결과 
	 * @throws Exception 오류
	 */
	public Workbook load( String filePath ) throws Exception{
		
		FileInputStream fis;
		POIFSFileSystem poiFs;
		Workbook wb;
		
		try{
			fis = new FileInputStream( filePath );
			poiFs = new POIFSFileSystem( fis );
			wb = new HSSFWorkbook( poiFs );	
		}catch (Exception e) {
			WLog.debug( "[ Error ] : WExcel. load [ file ] : " + e.toString() );
			throw new Exception( "[ Error ] : WExcel. load [ file ]  : " + e.toString() );
		}
		
		return wb;
	}
	
	public String[] getSheetNames( Workbook wb )  {
		
		ArrayList<String> sheetNames = new ArrayList<String>();
		int numOfSheets = wb.getNumberOfSheets();
		for( int i=0 ; i<numOfSheets ; i++ )
		{
			sheetNames.add( wb.getSheetName( i ) );
		}
		String[] rtnVal = new String[ sheetNames.size() ];
		
		return sheetNames.toArray( rtnVal );
	}
	
	
}
